package kr.co.itwise.se.ooad.banking;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;

public class BankingMgr {

	private List bankingList;
	private List transactionList;

	public BankingMgr() {
		this.bankingList = new ArrayList();
		this.transactionList = new ArrayList();
	}

	public void addaccount(AccountInfo accountinfo) {
		bankingList.add(accountinfo);
	}

	public void addtransactionallinfo(TransactionalInfo transationalinfo) {
		transactionList.add(transationalinfo);
	}

	public List serchAccountInfo(String keyword) {
		List serchbankingList = new ArrayList();
		for (int i = 0; i < this.bankingList.size(); i++) {
			AccountInfo accountinfo = (AccountInfo) this.bankingList.get(i);
			// System.out.println(keyword);
			// System.out.println(this.bankingList.get(i));
			// System.out.println(accountinfo.getAccountnumber());
			if (keyword.equals(accountinfo.getAccountnumber())
					|| keyword.equals(accountinfo.getAccountholder())) {
				serchbankingList.add(accountinfo);
			}
		}
		return serchbankingList;
	}

	public int serchBalance(String accountnumber) {
		// List resultbalance = new ArrayList();
		int balance = 0;
		for (int i = 0; i < this.bankingList.size(); i++) {
			AccountInfo accountinfo = (AccountInfo) this.bankingList.get(i);
			if (accountnumber.equals(accountinfo.getAccountnumber())) {
				balance = accountinfo.getBalance();
			}

		}
		return balance;

	}

	public void transBalance(String accountnumber, int balance, String bankname) {
		for (int i = 0; i < this.bankingList.size(); i++) {
			AccountInfo accountinfo = (AccountInfo) this.bankingList.get(i);
			if (accountnumber.equals(accountinfo.getAccountnumber())) {
				accountinfo.setBalance(accountinfo.getBalance() + balance);

				Date date = new Date();

				TransactionalInfo transationalinfo = new TransactionalInfo();
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				transationalinfo.transactiondate = df.format(date);
				SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss");
				transationalinfo.transactiontime = tf.format(date);

				transationalinfo.accountnumber = accountinfo.getAccountnumber();
				String depositwithdraw = "";
				if (balance < 0) {
					transationalinfo.depositwithdraw = "withdraw";
				} else if (balance > 0) {
					transationalinfo.depositwithdraw = "deposit";
				}

				transationalinfo.tradeamount = balance;
				transationalinfo.bankname = bankname;

				addtransactionallinfo(transationalinfo);

			}
		}

	}

	public List getTransactionalInfo() {
		return transactionList;
	}

	public List getAccountInfo() {
		return bankingList;
	}
}
